package com.wutuo.classroom.provider.service;

import cn.apestar.core.base.BaseService;
import cn.apestar.core.base.GenericMapper;
import cn.apestar.user.api.enums.UserType;
import cn.apestar.user.provider.institution.response.UserAndInstituionResponse;
import cn.apestar.user.provider.institution.service.UserAndInstitutionService;
import com.wutuo.classroom.api.LearningClassroomAPIService;
import com.wutuo.classroom.api.request.LearningClassroomRequest;
import com.wutuo.classroom.api.request.ListLearningClassroomRequest;
import com.wutuo.classroom.api.response.LearningClassroomResponse;
import com.wutuo.classroom.provider.dao.LearningClassroomMapper;
import com.wutuo.classroom.provider.domain.LearningClassroom;
import com.wutuo.common.UserAndInstitutionWTService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 学堂 <br>
 *
 * @author lvh
 * @version 1.0
 */
@Service
public class LearningClassroomService extends BaseService<LearningClassroom, Long>
        implements LearningClassroomAPIService {

    private Logger logger = LoggerFactory.getLogger(LearningClassroomService.class);

    @Autowired
    private LearningClassroomMapper learningClassroomMapper;

    @Autowired
    private UserAndInstitutionWTService userAndInstitutionWTService;

    private UserAndInstitutionService userAndInstitutionService;

    @Override
    protected GenericMapper<LearningClassroom, Long> getMapper() {

        // TODO Auto-generated method stub
        return learningClassroomMapper;
    }

    public void addorUpdate(LearningClassroomRequest record) {

        if (record.getId() == null) {
            if (list(record).size() == 0)
                super.add(record);
        } else
            super.update(record);
    }

    public void del(Long id) {

        LearningClassroomRequest model = new LearningClassroomRequest();
        model.setId(id);
        super.delete(model);
    }

    public List<LearningClassroomResponse> lisForPage(ListLearningClassroomRequest request) {

        List<LearningClassroomResponse> list = learningClassroomMapper.lisForPage(request);
        return list;
    }

    public List<LearningClassroomResponse> list(LearningClassroomRequest request) {

        return learningClassroomMapper.list(request);
    }

    /**
     * 根据登录单位获取当前可以登录的信息
     *
     * @param userId
     * @param request
     * @return List<LearningClassroomResponse>
     * @author lvh 2016年4月9日 上午11:08:06</br>
     */
    public List<LearningClassroomResponse> listLearningClassRoomByUserId(String org, Long userId,
                                                                         ListLearningClassroomRequest request) {

        StringBuffer type = new StringBuffer();
        if (UserType.PARENT.getValue().equals(org)) {
            type.append("'");
            type.append(UserType.PARENT.getValue());
            type.append("',");
        }
        List<UserAndInstituionResponse> userAndInstituionList = userAndInstitutionService
            .getRoleTypeListByUserId(userId);
        for (int i = 0; i < userAndInstituionList.size(); i++) {
            UserAndInstituionResponse userAndInstituionResponse = userAndInstituionList.get(i);
            if (UserType.MASTER.getValue().equals(userAndInstituionResponse.getUserType())) {
                type.append("'");
                type.append(UserType.MASTER.getValue());
                type.append("',");
            }
            if (UserType.TEACHER.getValue().equals(userAndInstituionResponse.getUserType())) {
                type.append("'");
                type.append(UserType.TEACHER.getValue());
                type.append("',");
            }
        }
        if (StringUtils.isEmpty(type.toString())) {
            return Collections.emptyList();
        }
        request.setLearningObject(type.toString());
        request.setDelFlag(true);
        List<LearningClassroomResponse> list = learningClassroomMapper.lisForPage(request);
        return list;
    }

    public LearningClassroom find(Long id) {
        return super.find(id);
    }

}